//
//  JXMallTableViewCell.m
//

#import "JXMallTableViewCell.h"

#import "NSString+Extends.h"

#define MallTableSpace 10
#define MallTableImageSide 100

@implementation JXMallTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier
              model:(JXItemModel *)model {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.good = model;
    }
    return self;
}

// getter methods

- (UIImageView *)goodImageView {
    if (!_goodImageView) {
        _goodImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_goodImageView];
    }
    return _goodImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont systemFontOfSize:16.f];
        _nameLabel.numberOfLines = 0;
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.textColor = [UIColor blackColor];
        [self.contentView addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)infoLabel {
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc] init];
        _infoLabel.font = [UIFont systemFontOfSize:12.f];
        _infoLabel.numberOfLines = 0;
        _infoLabel.textColor = [UIColor grayColor];
        [self.contentView addSubview:_infoLabel];
    }
    return _infoLabel;
}

- (UILabel *)discountInfoLabel {
    if (!_discountInfoLabel) {
        _discountInfoLabel = [[UILabel alloc] init];
        _discountInfoLabel.numberOfLines = 0;
        _discountInfoLabel.font = [UIFont systemFontOfSize:14.f];
        [self.contentView addSubview:_discountInfoLabel];
    }
    return _discountInfoLabel;
}

- (UILabel *)regularPriceLabel {
    if (!_regularPriceLabel) {
        _regularPriceLabel = [[UILabel alloc] init];
        _regularPriceLabel.numberOfLines = 0;
        _regularPriceLabel.font = [UIFont systemFontOfSize:14.f];
        [self.contentView addSubview:_regularPriceLabel];
    }
    return _regularPriceLabel;
}

- (UILabel *)discountPriceLabel {
    if (!_discountPriceLabel) {
        _discountPriceLabel = [[UILabel alloc] init];
        _discountPriceLabel.numberOfLines = 0;
        _discountPriceLabel.font = [UIFont systemFontOfSize:14.f];
        [self.contentView addSubview:_discountPriceLabel];
    }
    return _discountPriceLabel;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.goodImageView.frame =
            CGRectMake(MallTableSpace, MallTableSpace, MallTableImageSide, MallTableImageSide + 20);
    self.goodImageView.image = self.good.image;

    CGSize nameSize =
            [self.good.name calculateSize:CGSizeMake(200, 0) font:[UIFont systemFontOfSize:16.f]];
    self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.goodImageView.frame) + MallTableSpace,
                                      MallTableSpace, nameSize.width, nameSize.height);
    self.nameLabel.text = self.good.name;

    // info
    CGSize infoSize =
            [self.good.info calculateSize:CGSizeMake(200, 0) font:[UIFont systemFontOfSize:12.f]];
    self.infoLabel.frame = CGRectMake(CGRectGetMaxX(self.goodImageView.frame) + MallTableSpace,
                                      CGRectGetMaxY(self.nameLabel.frame) + MallTableSpace,
                                      infoSize.width, infoSize.height);
    self.infoLabel.text = self.good.info;

    // discount info
    CGSize discountInfoSize = [self.good.discountInfo calculateSize:CGSizeMake(200, 0)
                                                               font:[UIFont systemFontOfSize:14.f]];
    self.discountInfoLabel.frame =
            CGRectMake(CGRectGetMaxX(self.goodImageView.frame) + MallTableSpace,
                       CGRectGetMaxY(self.infoLabel.frame) + MallTableSpace, discountInfoSize.width,
                       discountInfoSize.height);
    self.discountInfoLabel.text = self.good.discountInfo;

    // discount price
    CGSize discountPriceSize =
            [self.good.discountPrice calculateSize:CGSizeMake(200, 0)
                                              font:[UIFont systemFontOfSize:14.f]];
    self.discountPriceLabel.frame =
            CGRectMake(CGRectGetMaxX(self.goodImageView.frame) + MallTableSpace,
                       CGRectGetMaxY(self.discountInfoLabel.frame) + MallTableSpace,
                       discountPriceSize.width, discountPriceSize.height);
    self.discountPriceLabel.text = self.good.discountPrice;

    // regular Price
    CGSize regularPriceSize = [self.good.regularPrice calculateSize:CGSizeMake(200, 0)
                                                               font:[UIFont systemFontOfSize:14.f]];
    self.regularPriceLabel.frame =
            CGRectMake(CGRectGetMaxX(self.goodImageView.frame) + MallTableSpace,
                       CGRectGetMaxY(self.discountPriceLabel.frame) + MallTableSpace,
                       regularPriceSize.width, regularPriceSize.height);
    self.regularPriceLabel.text = self.good.regularPrice;
}

@end
